﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Modeling.Shell;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Shell.Interop;
using Microsoft.VisualStudio;

namespace VsxFactory.Modeling
{
    /// <summary>
    /// Editor factory associé aux fichiers .diagram empechant ceux ci de s'ouvrir.
    /// Rajouter les 2 attributs suivants dans la délcaration du package :
    ///     [ProvideEditorFactory(typeof(DiagramNoDisplayEditorFactory), 103, TrustLevel = __VSEDITORTRUSTLEVEL.ETL_AlwaysTrusted)]
    ///     [ProvideEditorExtension(typeof(DiagramNoDisplayEditorFactory), Constants.DefaultDiagramExtension, Int32.MaxValue)]
    /// et la ligne suivant dans l'initialize du package :
    ///      this.RegisterEditorFactory(new DiagramNoDisplayEditorFactory());
    /// Attention, l'ajout par programmation d'un fichier diagram générera une exception qu'il faudra catcher.
    /// </summary>
    [Guid("82E6DEF1-E15D-468a-9B87-1DD2E17BF93B")]
    public class DiagramNoDisplayEditorFactory : IVsEditorFactory
    {
        #region IVsEditorFactory Members

        /// <summary>
        /// Closes this instance.
        /// </summary>
        /// <returns></returns>
        public int Close()
        {
            return VSConstants.S_OK;
        }

        /// <summary>
        /// Creates the editor instance.
        /// </summary>
        /// <param name="grfCreateDoc">The GRF create doc.</param>
        /// <param name="pszMkDocument">The PSZ mk document.</param>
        /// <param name="pszPhysicalView">The PSZ physical view.</param>
        /// <param name="pvHier">The pv hier.</param>
        /// <param name="itemid">The itemid.</param>
        /// <param name="punkDocDataExisting">The punk doc data existing.</param>
        /// <param name="ppunkDocView">The ppunk doc view.</param>
        /// <param name="ppunkDocData">The ppunk doc data.</param>
        /// <param name="pbstrEditorCaption">The PBSTR editor caption.</param>
        /// <param name="pguidCmdUI">The pguid CMD UI.</param>
        /// <param name="pgrfCDW">The PGRF CDW.</param>
        /// <returns></returns>
        public int CreateEditorInstance(uint grfCreateDoc, string pszMkDocument, string pszPhysicalView, IVsHierarchy pvHier, uint itemid, IntPtr punkDocDataExisting, out IntPtr ppunkDocView, out IntPtr ppunkDocData, out string pbstrEditorCaption, out Guid pguidCmdUI, out int pgrfCDW)
        {
            ppunkDocView = IntPtr.Zero;
            ppunkDocData = IntPtr.Zero;
            pguidCmdUI = GetType().GUID;
            pgrfCDW = 0;
            pbstrEditorCaption = null;

            // On ne veut pas l'ouvrir
            return VSConstants.VS_E_INCOMPATIBLEDOCDATA;
        }

        /// <summary>
        /// Maps the logical view.
        /// </summary>
        /// <param name="rguidLogicalView">The rguid logical view.</param>
        /// <param name="pbstrPhysicalView">The PBSTR physical view.</param>
        /// <returns></returns>
        public int MapLogicalView(ref Guid rguidLogicalView, out string pbstrPhysicalView)
        {
            pbstrPhysicalView = null;
            return VSConstants.S_OK;
        }

        /// <summary>
        /// Sets the site.
        /// </summary>
        /// <param name="psp">The PSP.</param>
        /// <returns></returns>
        public int SetSite(Microsoft.VisualStudio.OLE.Interop.IServiceProvider psp)
        {
            return VSConstants.S_OK;
        }

        #endregion
    }
}
